package com.sky.aspect;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 自动记录操作日志
 */
@Aspect
@Component
@Slf4j
public class AutoLogAspect {

    @Pointcut("execution(* com.sky.service.*.*(..)) && @annotation(com.sky.annotation.Log)")
    public void autoLogPointCut(){}


    public Object autoFillAdvice(ProceedingJoinPoint joinPoint){
        log.info("自动记录操作日志...");

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String name = signature.getMethod().getName(); // 被拦截的方法名

        long begin = System.currentTimeMillis();

        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        long end = System.currentTimeMillis();

        log.info("被拦截的方法为：{}，业务超时：{}",name,end - begin);
        return proceed;
    }
}
